package com.djinfo.work.service.impl;

import com.djinfo.work.common.util.StringUtil;
import com.djinfo.work.dao.NewsDao;
import com.djinfo.work.entity.SysNewsEntity;
import com.djinfo.work.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * Created by gz on 2017/5/10.
 */
@Service("newsService")
public class NewsServiceImpl implements NewsService {

    @Autowired
    private NewsDao newsDao;

    private final static String selAllHql=" from SysNewsEntity where 1=1 ";

    @Override
    public void saveNews(SysNewsEntity news, String uesrId) {
        if(news.getId()==null){
            news.setLikeNum(0);
            news.setCreateTime(StringUtil.getNowTime());
            news.setUpdateTime(StringUtil.getNowTime());
            news.setCteateUser(uesrId);
            news.setUpdateUser(uesrId);
            newsDao.saveNews(news);
        }else{
            SysNewsEntity sysNews = newsDao.getNewsById(news.getId());
            sysNews.setUpdateTime(StringUtil.getNowTime());
            sysNews.setContent(news.getContent());
            sysNews.setAuthor(news.getAuthor());
            sysNews.setImg(news.getImg());
            sysNews.setNewsType(news.getNewsType());
            sysNews.setTitle(news.getTitle());
            sysNews.setUpdateUser(uesrId);
            newsDao.saveNews(sysNews);

        }
    }

    @Override
    public HashMap<String, Object> getPage(SysNewsEntity news, Integer pageNum, Integer pageSize) {
        HashMap<String, Object> map = new HashMap<>();
        List<SysNewsEntity> res = page(news,pageNum,pageSize);
        Integer count = getAllRowCount(news);
        map.put("dataList",res);
        map.put("total",count);
        return map;
    }

    @Override
    public List<SysNewsEntity> page(SysNewsEntity tbSysNews, Integer pageNum, Integer pageSize) {
        String hql = selAllHql;
        if(tbSysNews.getTitle()!=null && !"".equals(tbSysNews.getTitle())){
            hql+= " and title like '%" + tbSysNews.getTitle() + "%' or author like '%" + tbSysNews.getTitle() + "%'";
        }
        if(tbSysNews.getNewsType()!=null){
            hql+= " and newsType = " + tbSysNews.getNewsType() + " order by createTime desc ";
        }
        return newsDao.page(hql,  pageNum,  pageSize);
    }

    @Override
    public List<SysNewsEntity> list(SysNewsEntity news) {
        return newsDao.list(news);
    }

    @Override
    public Integer getAllRowCount(SysNewsEntity news) {
        String hql = " select count(*) from SysNewsEntity where 1=1 ";
        if(news.getTitle()!=null && !"".equals(news.getTitle())){
            hql+= " and title like '%" + news.getTitle() + "%' or author like '%" + news.getTitle() + "%'";
        }

        if(news.getNewsType()!=null){
            hql+= " and newsType = " + news.getNewsType() + "  ";
        }

        return newsDao.getAllRowCount(hql);
    }

    @Override
    public void update(SysNewsEntity news) {
        newsDao.update(news);
    }

    @Override
    public void delete(SysNewsEntity news) {
        newsDao.delete(news);
    }

    @Override
    public SysNewsEntity selectOne(SysNewsEntity news) {
        return newsDao.selectOne( news);
    }

    @Override
    public SysNewsEntity getNewsById(String newsId) {
        return newsDao.getNewsById(newsId);
    }
}
